<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>回调页面</title>
  <script src="./plugins/vue/dist/vue.js"></script>
  <script src="./plugins/axios/dist/axios.js"></script>
  <script src="./script/common.js"></script>
</head>
<body>
  <div id="app"></div>
  <script>
    new Vue({
        el:"#app",
        data:{

        },
        mounted(){
            // 解析url，拿到微信传给我的授权code
            let urlObj = parseUrl();
            // 调用后端接口，通过授权code,获取微信的accessToken和用户的openid
            
            console.log("回调数据",urlObj);
            this.$http.post("/logininfo/wechat/login/"+urlObj.code)
                .then(result => {
                    result = result.data;
                    if(result.success){
                      if(result.data.loginUser){
                        // 说明后端已经绑定，并且已经做了免密登录
                        let {token,loginUser} = result.data;
                        localStorage.setItem("loginUser", JSON.stringify(loginUser));
                        localStorage.setItem("token", token);
                        location.href = "/index.html";
                      }else{
                        // 说明该微信用户还没有绑定，需要跳转到绑定页面进行绑定
                        console.log("未登录过",result);
                        let {access_token,openid} = result.data;
                        location.href = "/binder.html?openid="+openid+"&accessToken="+access_token;
                      }
                    }else{
                        alert(result.message)
                    }
                }).catch(result => {

                })
        }
      })
  </script>
</body>
</html>